package service

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"inventory/api/inventory"
	"inventory/pkg/ecode"
	"inventory/pkg/util"
	"time"
)

// 健康检查
func (s *Service) HealthCheck(ctx context.Context, _ *empty.Empty) (resp *inventory.DefaultResp, err error) {
	if err = s.dao.Ping(ctx); err == nil {
		resp = &inventory.DefaultResp{Status: true}
	} else {
		resp = &inventory.DefaultResp{
			ErrorCode:    ecode.ErrSystemError.CodeMessage,
			ErrorMessage: err.Error(),
		}
	}
	return
}

// 校验日期参数
func verifyStartDateAndEndDate(rawStartDate, rawEndDate string) (startDate, endDate time.Time, err error) {
	if startDate, err = util.ParseTime(rawStartDate); err != nil {
		err = ecode.BadRequest("startDate:" + rawStartDate)
		return
	}
	if endDate, err = util.ParseTime(rawEndDate); err != nil {
		err = ecode.BadRequest("endDate:" + rawEndDate)
		return
	}
	if endDate.Sub(startDate).Hours()/24 > 30 {
		err = ecode.BadRequest("跨度不超过1个月")
		return
	}
	return
}
